package com.wxy.controller;

import com.wxy.config.RabbitMqProducer;
import com.wxy.dao.UserMapper;
import com.wxy.domain.User;
import com.wxy.service.RabbitMQService;
import com.wxy.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

/**
 * @author 47868
 */
@Controller
@RequestMapping("/mq")
public class MqController {
    @Autowired
    private UserService userService;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RabbitMqProducer rabbitMqProducer;

    @Operation(summary = "根据id更新密码",description = "检验rabbitmq运行是否成功")
    @ApiResponse(responseCode = "200",description = "成功",content = @Content)
    @GetMapping("/updateById/{id}")
    @ResponseBody
    public User updateById(@PathVariable("id") int id) throws InterruptedException {
        //查询用户
        User user=userService.findById(id);
        //修改密码
        String password=user.getPassword();
        int newPassword=Integer.parseInt(password)+1111;
        String newPwdString=Integer.toString(newPassword);
        //创建发送数据
        Map<String,Object> maps=new HashMap<>();
        maps.put("id",id);
        maps.put("password",newPwdString);
        //发送消息
        rabbitTemplate.convertAndSend("amq.direct","yellow",maps);
        Thread.sleep(1000);
        return userMapper.findById(id);
    }

    @GetMapping("/test/{id}")
    @ResponseBody
    public void test(@PathVariable("id") int id){
        Map<String,Object> maps=new HashMap<>();
        maps.put("id",Integer.toString(id));
        rabbitMqProducer.sendMapMessage("amq.direct","blue",maps);
    }
}
